package com.tianfeng.fenghuotoutiao.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mobads.AdView;
import com.baidu.mobads.AdViewListener;
import com.baidu.mobads.AppActivity;
import com.db.ta.sdk.NonStandardTm;
import com.db.ta.sdk.NsTmListener;
import com.db.ta.sdk.TmActivity;
import com.google.gson.Gson;
import com.tianfeng.fenghuotoutiao.R;
import com.tianfeng.fenghuotoutiao.globle.ConstantAd;
import com.tianfeng.fenghuotoutiao.net.bean.ResTaCustomAdBean;
import com.tianfeng.fenghuotoutiao.ui.activity.base.BaseActivity;
import com.tianfeng.fenghuotoutiao.utils.ImageUtils;

import org.json.JSONObject;

/**
 * Created by Dinosa on 2018/1/30.
 */

public class RedPacketDialog  {

    public static AlertDialog alertDialog=null;
    public static NonStandardTm mNonStandardTm=null;

    public static DialogInterface.OnDismissListener sOnDismissListener;

    static AdView adView;
    public static void show (final Context context, String goldNumber, final DialogInterface.OnDismissListener mListener){

        //红包的对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.NoBackGroundDialog);
        View view = View.inflate(context, R.layout.dialog_red_packet, null);
        TextView titleView = (TextView) view.findViewById(R.id.tv_dialog_title);

        String title="<big>恭喜您</big><br/>获得 <big><font color='"+context.getResources().getColor(R.color.dialog_red_pack_number_text_color)+"'>"+goldNumber+"</font></big> 金币";

        titleView.setText(Html.fromHtml(title));
        /*420*240 自定义广告大小*/

        final RelativeLayout bannerView = (RelativeLayout) view.findViewById(R.id.ll_ad);

//
//        if(((BaseActivity) context).isHasAds()){
//            mNonStandardTm = new NonStandardTm(context);
//            mNonStandardTm.loadAd(ConstantAd.BAIDUAD.STIMULATE_VIDEO);
//            mNonStandardTm.setAdListener(new NsTmListener() {
//                @Override
//                public void onReceiveAd(String s) {
//
//                    BaseActivity activity = (BaseActivity) context;
//                    if (activity == null || activity.isFinishing()) {
//                        return;
//                    }
//
//                    Gson gson = new Gson();
//                    final ResTaCustomAdBean resTaCustomAdBean = gson.fromJson(s, ResTaCustomAdBean.class);
//
//                    //这里是加载图片的问题；
//                    try {
//                        ImageUtils.loadImage(context,resTaCustomAdBean.getImg_url(),bannerView);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//
//                    if (mNonStandardTm != null) {
//                        mNonStandardTm.adExposed();
//                    }
//
//                    bannerView.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            if (mNonStandardTm != null) {
//                                mNonStandardTm.adClicked();
//                            }
//                            //这里还是需要做对应的操作；
//                            TmActivity.a(context,resTaCustomAdBean.getClick_url());
//                        }
//                    });
//                }
//
//                @Override
//                public void onFailedToReceiveAd() {
//                    System.out.println("获取失败2333");
//                }
//            });
//        }
//        bannerView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });


        AppActivity
                .setActionBarColorTheme(AppActivity.ActionBarColorTheme.ACTION_BAR_WHITE_THEME);

        // 创建广告View
        String adPlaceId = "6214113"; //  重要：请填上您的广告位ID，代码位错误会导致无法请求到广告
        adView = new AdView((BaseActivity) context, adPlaceId);
        // 设置监听器
        adView.setListener(new AdViewListener() {
            public void onAdSwitch() {
                Log.w("", "onAdSwitch");
            }

            public void onAdShow(JSONObject info) {
                // 广告已经渲染出来
                Log.w("", "onAdShow " + info.toString());
            }

            public void onAdReady(AdView adView) {
                // 资源已经缓存完毕，还没有渲染出来
                Log.w("", "onAdReady " + adView);
            }

            public void onAdFailed(String reason) {
                Log.w("", "onAdFailed " + reason);
            }

            public void onAdClick(JSONObject info) {
                // Log.w("", "onAdClick " + info.toString());

            }

            @Override
            public void onAdClose(JSONObject arg0) {
                Log.w("", "onAdClose");
            }
        });

        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager)  context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
        int winW = dm.widthPixels;
        int winH = dm.heightPixels;
        int width = Math.min(winW, winH);
        int height = width * 3 / 7;
        // 将adView添加到父控件中(注：该父控件不一定为您的根控件，只要该控件能通过addView能添加广告视图即可)
        RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(width, height);
        rllp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        bannerView.addView(adView, rllp);





        view.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (alertDialog != null) {
                    alertDialog.dismiss();
                    //这里是点击了确定的操作；
                }
            }
        });

        builder.setView(view);
        alertDialog= builder.create();
        if (mListener != null) {
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    mListener.onDismiss(dialog);
                    if (mNonStandardTm != null) {
                        mNonStandardTm.destroy();
                        mNonStandardTm=null;
                    }
                }
            });
        }

        alertDialog.show();
    }

    public static void show (Context context, String goldNumber){
        show(context,goldNumber,null);
    }

}
